package com.estore.action;

import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

import com.estore.converter.DateTime;
import com.estore.converter.MD5;
import com.estore.info.RegisterInfo;
import com.estore.manager.ProfileManager;
import com.estore.model.RegisterModel;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class RegisterAction extends ActionSupport implements
		ModelDriven<RegisterModel> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4881829561292330303L;
	RegisterModel registerModel = new RegisterModel();
	ProfileManager profileManager;
	DateTime dateTime = new DateTime();

	public String register() {
		boolean check = true;
		registerModel.setErrorAccount(null);
		registerModel.setErrorEmail(null);
		registerModel.setRegisterSuccess(null);
		RegisterInfo registerInfo = registerModel.getRegisterInfo();
		Integer year = Integer.parseInt(registerModel.getYear()) - 1900;
		String currentDate = dateTime.currentDate();
		String getDate = dateTime.getDate(year,
				Integer.parseInt(registerModel.getMonth()),
				Integer.parseInt(registerModel.getDay()));
		if (registerInfo.getAccount().equals("")
				|| registerInfo.getPassword().equals("")
				|| registerInfo.getFirstName().equals("")
				|| registerInfo.getLastName().equals("")
				|| registerInfo.getEmail().equals("")
				|| registerInfo.getSecretAnswer().equals("")
				|| registerModel.getDay().equals("-1")
				|| registerModel.getMonth().equals("-1")
				|| registerModel.getYear().equals("-1")
				|| registerInfo.getSecretQuestion().equals("-1")) {
			check = false;
		}
		System.out.println(registerInfo.getAccount());
		if (registerModel.getRole() == null) {
			registerInfo.setRole(4);
		} else {
			registerInfo.setRole(registerModel.getRole());
		}
		registerInfo.setDateOfBirth(getDate);
		registerInfo.setRegDate(currentDate);
		String gentle = null;
		if (registerModel.getSex().equals("-1")) {
			gentle = "male";
		} else if (registerModel.getSex().equals("1")) {
			gentle = "female";
		}
		registerInfo.setSex(gentle);
		if (check == true) {
			if (profileManager.checkAccountAvail(registerInfo.getAccount())
					&& profileManager.checkEmailAvail(registerInfo.getEmail())) {
				registerModel
						.setErrorAccount("Tài khoản đã được sử dụng");
				registerModel.setErrorEmail("Email đã được sử dụng ");
				if (registerModel.getRole() == null) {
					return "error";
				} else {
					return "modError";
				}

			} else if (!profileManager.checkAccountAvail(registerInfo
					.getAccount())
					&& profileManager.checkEmailAvail(registerInfo.getEmail())) {
				registerModel.setErrorEmail("Email đã được sử dụng ");
				if (registerModel.getRole() == null) {
					return "error";
				} else {
					return "modError";
				}
			} else if (profileManager.checkAccountAvail(registerInfo
					.getAccount())
					&& !profileManager.checkEmailAvail(registerInfo.getEmail())) {
				registerModel
						.setErrorAccount("Tài khoản đã được sử dụng");
				if (registerModel.getRole() == null) {
					return "error";
				} else {
					return "modError";
				}
			} else {
				registerModel
						.setRegisterSuccess("Chúc mừng bạn đã đăng kí thành công");
				String pass = registerInfo.getPassword();
				registerInfo.setPassword(MD5.encryptMD5(pass));
				profileManager.register(registerInfo);
				RegisterInfo info = new RegisterInfo(null, null, null, null,
						null, null, null);
				registerModel.setRegisterInfo(info);
				if (registerModel.getRole() == null) {
					return "success";
				} else {
					return "modSuccess";
				}
			}
		} else {
			if (registerModel.getRole() == null) {
				return "error";
			} else {
				return "modError";
			}
		}
	}

	@Override
	public RegisterModel getModel() {
		// TODO Auto-generated method stub
		return registerModel;
	}

	public RegisterModel getRegisterModel() {
		return registerModel;
	}

	public void setRegisterModel(RegisterModel registerModel) {
		this.registerModel = registerModel;
	}

	public ProfileManager getProfileManager() {
		return profileManager;
	}

	public void setProfileManager(ProfileManager profileManager) {
		this.profileManager = profileManager;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

}
